﻿using System.Web.Mvc;
using Netkk.Common;
using System.Web.Routing;

namespace Netkk.Core.Auth
{
    public class UserInRoleAttribute : ActionFilterAttribute
    {
        public Roles Role { get; private set; }
        
        public UserInRoleAttribute(Roles role)
        {
            Role = role;
        }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            CustomPrincipal prinzipal = new CustomPrincipal(filterContext.HttpContext.User.Identity);
            if (prinzipal.IsInRole(Role))
            {
                return;
            }
            
            RouteValueDictionary dictionary = new RouteValueDictionary
                                                  {
                                                      {"controller", "Content"}, 
                                                      {"action", "Show"}, 
                                                      {"id", "AccessDenied"}
                                                  };
            filterContext.Result = new RedirectToRouteResult(dictionary); ;
        }
    }
}